//
//  NewsHeadViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/7/8.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit


let MenuHeight: CGFloat = 43
let HeadViewHeight: CGFloat = 253

enum HeadShowType {
    case All
    case OnlyAds
    case OnlyDropMenu
    case None
}

protocol HeardDelegate {
    func onHeightChange(newView: UIView) -> Void
    func setContentViewControllerWithInfo() -> Void
    func setContentTransferViewControllerWithInfo() -> Void
    func setContentJoninUsViewControllerWithInfo() -> Void
    func setContentMXQYViewConreollerWithInfo() -> Void
    func setContentZYCPViewControllerWithInfo() -> Void
    func CyclePictureViewSelected(url: String)
    
//    func onHeightChange(newView: UIView) -> Void
//    func setContentViewControllerWithInfo(cellInfo: [NewsModel]? ) -> Void
//    func setContentTransferViewControllerWithInfo(cellInfo: [TransferModel]?) -> Void
//    func setContentJoninUsViewControllerWithInfo(cellInfo: [JoinUsModel]?) -> Void
//    func setContentMXQYViewConreollerWithInfo(cellInfo: [starBusinessInfo]?) -> Void
//    func setContentZYCPViewControllerWithInfo(cellInfo: [specialProductInfo]?) -> Void
//    func CyclePictureViewSelected(url: String)

}

class NewsHeadViewController: UIViewController {

    @IBOutlet weak var cyclePicture: CyclePictureView!
    @IBOutlet weak var JSDropMenuView: UIView!
    @IBOutlet weak var JSDropMenuViewButtomCons: NSLayoutConstraint!
    @IBOutlet weak var JSDropMenuViewHeightConst: NSLayoutConstraint!
    var dropDownMenu: JSDropDownMenu?
    
    var showType: HeadShowType = .All
    
    var delegate: HeardDelegate?
    
    //产品推荐、行业资讯、享乐美学
    var tages: String?
    var paixu: String?
    //业内转让
    var type: String?
    var area: String?
    var shaixuan:String?
    //求职招聘
    var position: String?//目的
    var unitarea: String?//区域
    var positiontype: String?//职位
    var pay: String?//薪资
    //明星企业
    var membertypecn: String?//企业分类
    var hyfl: String?//行业分类
    var reputation: String?//筛选
    //专业产品
    var productTags: String?//产品分类
    var productReputation: String?//行业美誉
    var productPaixu: String?//排序
    
    var model: AdvertModel? {
        didSet {
            if model?.pictureList.count > 0 {
                var images: [String] = []
                for item in model!.pictureList {
                    images.append(item.imgsrc)
                }
                self.cyclePicture.imageURLArray = images
                
                if model?.dropDownList.count == 0 {
                    showType = .OnlyAds
                } else {
                    showType = .All
                }
            } else {
                if model?.dropDownList.count > 0 {
                    showType = .OnlyDropMenu
                } else {
                    showType = .None
                }
            }
            
            if (self.dropMenuSelectIndex.count > 0) {
                self.dropMenuSelectIndex.removeAll()
            }
            if model != nil{
                for _ in (model?.dropDownList)! {
                    self.dropMenuSelectIndex.append(0)
                }
                
            }
            
            if model?.dropDownList.count > 0 {
                self.dropDownMenu!.dataSource = self
            }
           
            self.setupView()
        }
    }
    
    var dropMenuSelectIndex: [Int] = []
    
    var url: String? {
        didSet {
            DataTool.loadAdvertData(url!, completionHandler: { (newModel) in
                // todo reload picture and dropdown
                self.model = newModel
            })
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        cyclePicture.delegate = self
        
        dropDownMenu = JSDropDownMenu.init(origin: CGPointMake(0, 1), andHeight: MenuHeight)
    
        dropDownMenu!.delegate = self
      
        self.JSDropMenuView.addSubview(dropDownMenu!)
        self.cyclePicture.autoScroll = true
        self.cyclePicture.timeInterval = 2
        self.cyclePicture.detailLableBackgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
    }
    
    func initDropDownMenu() {
        dropDownMenu = JSDropDownMenu.init(origin: CGPointMake(0, 1), andHeight: MenuHeight)
        dropDownMenu!.delegate = self
        //self.view.addSubview(dropDownMenu!)
        self.JSDropMenuView.addSubview(dropDownMenu!)
        self.cyclePicture.autoScroll = true
        self.cyclePicture.timeInterval = 2
        self.cyclePicture.detailLableBackgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.5)
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setupView() -> Void {
        switch showType
        {
        case .OnlyAds:
            self.dropDownMenu?.hidden = true
            self.JSDropMenuView.hidden = true
            self.view.frame.size.height = cyclePicture.size.height
        case .OnlyDropMenu:
            
            self.cyclePicture.hidden = true
            self.cyclePicture.frame.size.height = 0
            self.JSDropMenuView.hidden = false
            self.JSDropMenuViewButtomCons.constant = 0
            self.JSDropMenuView.layoutIfNeeded()
            self.view.frame.size.height = MenuHeight + 2
            
        case .None:
            self.dropDownMenu?.hidden = true
            self.cyclePicture.hidden = true
            
            self.view.frame.size.height = 0
        default:
            self.dropDownMenu?.hidden = false
            self.JSDropMenuView.hidden = false
            self.dropDownMenu?.frame.origin.y = 1
            self.view.frame.size.height = cyclePicture.size.height + MenuHeight + 2
        }
        
        delegate?.onHeightChange(self.view)
    }
}

extension NewsHeadViewController: JSDropDownMenuDataSource,JSDropDownMenuDelegate {
    
    
    func numberOfColumnsInMenu(menu: JSDropDownMenu!) -> Int {
        return self.dropMenuSelectIndex.count
    }
    
    func displayByCollectionViewInColumn(column: Int) -> Bool {
//        if column == 1 {
//            return true
//        }
        
        return false
    }
    
    func haveRightTableViewInColumn(column: Int) -> Bool {
        
        return false
    }
    
    func widthRatioOfLeftColumn(column: Int) -> CGFloat {
        return 1
    }
    func widthRatioOfRightColumn(column: Int) -> CGFloat {
        return 1
    }
    func currentLeftSelectedRow(column: Int) -> Int {
        
        return self.dropMenuSelectIndex[column]
    }
    
    func menu(menu: JSDropDownMenu!, numberOfRowsInColumn column: Int, leftOrRight: Int, leftRow: Int) -> Int {
        
        return self.model!.dropDownList[column].count
    }
    
    func menu(menu: JSDropDownMenu!, titleForColumn column: Int) -> String! {
        
        return self.model!.dropDownList[column][self.dropMenuSelectIndex[column]]
    }
    
    func menu(menu: JSDropDownMenu!, didSelectRowAtIndexPath indexPath: JSIndexPath!) {
        
        self.dropMenuSelectIndex[indexPath.column] = indexPath.row
        if self.url == "cptj"{
            if indexPath.column == 0{
                self.tages = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.paixu = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if self.tages == nil{
                self.tages = "全部分类"
            }
            if self.paixu == nil{
                self.paixu = "筛选排序"
            }
             self.delegate?.setContentViewControllerWithInfo()
//            let urlStr = String(format: "%@product/dosearch",BaseUrl)
//            DataTool.dropMenuScreen_cptj(self.tages!, paixu: self.paixu!,url: urlStr,page: "0", completionHandler: { (newsArray) -> Void in
//                guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentViewControllerWithInfo(newDataes)
//            })

            
        }else if self.url == "hyzx"{
            if indexPath.column == 0{
                self.tages = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.paixu = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if self.tages == nil{
                self.tages = "内容分类"
            }
            if self.paixu == nil{
                self.paixu = "智能排序"
            }
            self.delegate?.setContentViewControllerWithInfo()
//            let urlStr = String(format: "%@infomation/dosearch",BaseUrl)
//            DataTool.dropMenuScreen_cptj(self.tages!, paixu: self.paixu!,url: urlStr, page: "0", completionHandler: { (newsArray) -> Void in
//                guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentViewControllerWithInfo()
//            })
        }else if self.url == "xlmx"{
            if indexPath.column == 0{
                self.tages = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.paixu = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if self.tages == nil{
                self.tages = "分类"
            }
            if self.paixu == nil{
                self.paixu = "筛选排序"
            }
            self.delegate?.setContentViewControllerWithInfo()

//            let urlStr = String(format: "%@xlmx/dosearch",BaseUrl)
//            DataTool.dropMenuScreen_cptj(self.tages!, paixu: self.paixu!,url: urlStr, page: "0", completionHandler: { (newsArray) -> Void in
//                guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentViewControllerWithInfo(newDataes)
//            })
//
            
        }else if self.url == "ynzr"{
            if indexPath.column == 0{
                self.type = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.area = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 2{
                self.shaixuan = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if self.type == nil{
                self.type = "类别"
            }
            if self.area == nil{
                self.area = "区域"
            }
            if self.shaixuan == nil{
                self.shaixuan = "筛选"
            }
            self.delegate?.setContentTransferViewControllerWithInfo()
//            DataTool.dropMenuScreen_ynzr(self.type!, area: self.area!, shaixuan: self.shaixuan!,page: "0", completionHandler: { (newsArray) -> Void in
//                guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentTransferViewControllerWithInfo(newDataes)
//            })
        }else if self.url == "qzzp"{

            if indexPath.column == 0{
                self.position = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.unitarea = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 2{
                self.positiontype = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 3{
                self.pay = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if self.position == nil{
                self.position = "目的"
            }
            if self.unitarea == nil{
                self.unitarea = "区域"
            }
            if self.positiontype == nil{
                self.positiontype = "职位"
            }
            if self.pay == nil{
                self.pay = "薪资"
            }
        self.delegate?.setContentJoninUsViewControllerWithInfo()
//            DataTool.dropMenuScreen_qzzp(self.position!, unitarea: self.unitarea!, positiontype:self.positiontype! , pay: self.pay!,page: "0", completionHandler: { (newsArray) -> Void in
//                guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentJoninUsViewControllerWithInfo(newDataes)
//            })

        }else if self.url == "mxqy"{
            if indexPath.column == 0{
                self.membertypecn = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.hyfl = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 2{
                self.reputation = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if self.membertypecn == nil{
                self.membertypecn = "企业分类"
            }
            if self.hyfl == nil{
                self.hyfl = "行业分类"
            }
            if self.reputation == nil{
                self.reputation = "筛选"
            }
            self.delegate?.setContentMXQYViewConreollerWithInfo()
//            DataTool.dropMenuScreen_mxqy(self.membertypecn!, hyfl: self.hyfl!, reputation: self.reputation!,page: "0", completionHandler: { (newsArray) -> Void in
//                guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentMXQYViewConreollerWithInfo(newDataes)
//            })

        }else if self.url == "zycp"{

            if indexPath.column == 0{
                self.productTags = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 1{
                self.productReputation = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            if indexPath.column == 2{
                self.productPaixu = self.model!.dropDownList[indexPath.column][indexPath.row]
            }
            
            if self.productTags == nil{
                self.productTags = "产品分类"
            }
            if self.productReputation == nil{
                self.productReputation = "行业美誉"
            }
            if self.productPaixu == nil{
                self.productPaixu = "筛选"
            }
            self.delegate?.setContentZYCPViewControllerWithInfo()
//            DataTool.dropMenuScreen_zycp(self.productTags!, reputation: self.productReputation!, paixu: self.productPaixu!,page: "0", completionHandler: { (newsArray) -> Void in
//                 guard let newDataes = newsArray else {
//                    return
//                }
//                print(newDataes)
//                self.delegate?.setContentZYCPViewControllerWithInfo(newDataes)
//           })
        }

        
    }
    
    func menu(menu: JSDropDownMenu!, titleForRowAtIndexPath indexPath: JSIndexPath!) -> String! {
        
        return self.model!.dropDownList[indexPath.column][indexPath.row]
    }
    
    func menu(menu: JSDropDownMenu!, showViewHeight height: CGFloat) {
        self.JSDropMenuViewHeightConst.constant = height + 45
        self.JSDropMenuView.layoutIfNeeded()
        
        self.view.frame.size.height += height
       
        
    }
    
    func menuClosed(menu: JSDropDownMenu!) {
        switch showType {
        case .All:
            self.view.frame.size.height = HeadViewHeight
        case .OnlyDropMenu:
            self.view.frame.size.height = MenuHeight + 2
        case .OnlyAds:
            self.view.frame.size.height = self.cyclePicture.size.height
        default:
            break
        }
        
    }
}
extension NewsHeadViewController : CyclePictureViewDelegate {
    func cyclePictureView(cyclePictureView: CyclePictureView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        print("cyclePicture select")
        delegate?.CyclePictureViewSelected((self.model?.pictureList[indexPath.row].url)!)
    }
}